package com.travel.common.util;

import com.travel.common.CommonConstant;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/29 14:45
 * @Desc:
 */
public class CommonUtil {

    private static final BigDecimal HUNDRED = new BigDecimal("100");

    public static final String[] DAYS_CHINESE = {"一","二","三","四","五","六","七","八","九","十"};

    public static final Integer[] DAYS_INTEGER = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);

    public static final List<String> AREA_LIST = Arrays.asList("北京","天津","上海","重庆","河北","山西","辽宁","吉林","黑龙江","江苏","浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东","海南","四川","贵州","云南","陕西","甘肃","青海","台湾","内蒙古","广西","西藏","宁夏","新疆","香港","澳门");

    /**
     * 字符串id数组转整型id list
     * @param idArray
     * @return
     */
    public static List<Integer> idArrayToList(String[] idArray){
        List<Integer> idList = new ArrayList<>();
        for(String idStr : idArray){
            Integer id = Integer.parseInt(idStr);
            idList.add(id);
        }
        return idList;
    }

    public static List<Integer> idsToList(String ids){
        String[] idArray = StringUtils.split(ids, CommonConstant.COMMON_STR);
        return idArrayToList(idArray);
    }

    /**
     * 分转元
     * @param fen
     * @return
     */
    public static BigDecimal fenToYuan(Long fen){
        return new BigDecimal(fen).divide(HUNDRED, 2,BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 分转元
     * @param fen
     * @return
     */
    public static BigDecimal fenToYuan(Integer fen){
        return new BigDecimal(fen).divide(HUNDRED, 2,BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 元转分
     * @param yuan
     * @return
     */
    public static BigDecimal yuanToFen(BigDecimal yuan){
        return yuan.multiply(HUNDRED);
    }

    /**
     * 根据中文数字转Integer
     * @param days
     * @return
     */
    public static Integer getDays(String days){
        for(int i = 0; i < DAYS_CHINESE.length; i++){
            if(DAYS_CHINESE[i].equals(String.valueOf(days.charAt(0)))){
                return  DAYS_INTEGER[i];
            }
        }
        return null;
    }

    /**
     * 计算百分比，保留两位小数（为0则去掉）
     * @param profit
     * @param total
     * @return
     */
    public static String getProportion(Integer profit, Integer total){

        if(total == 0){
            return "0%";
        }

        BigDecimal proportion = new BigDecimal(profit).divide(new BigDecimal(total), 4, BigDecimal.ROUND_HALF_UP).multiply(ONE_HUNDRED).setScale(2, BigDecimal.ROUND_HALF_UP);

        NumberFormat nf = NumberFormat.getInstance();

        //百分比没有到千位，去不去掉千分符都一样
        return nf.format(proportion) + "%";
    }

    public static String getProportion(BigDecimal profit, BigDecimal total){
        profit = profit.multiply(new BigDecimal("100"));
        total = total.multiply(new BigDecimal("100"));
        return getProportion(profit.intValue(), total.intValue());
    }
}
